/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view;


import utils.XMLParser;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Hashtable;
import java.util.Vector;

/**
 *
 * @author Kvest
 */
public class ViewManager
{
    private static ViewManager manager;

    private Hashtable formsList;
    private String xml;
    private Vector formsXML;
    private final String xmlPath = "/xml/forms.xml";

    private int menutype = -1;

    private ViewManager()
    {
        formsList = new Hashtable();

        //Загружаем xml
        loadXML();
    }

    public static ViewManager getViewManager()
    {
        if (manager == null)
        {
            manager = new ViewManager();
        }

        return manager;
    }

    public String getTenMinutesText()
    {
        if (getFunctionMenuType() == FunctionMenu.CHERNOVTSI_TYPE)
        {
            return "5";
        }
        else
        {
            return "10";
        }
    }

    public int getFunctionMenuType()
    {
        if (menutype == -1)
        {
            menutype = XMLParser.getIntElement(xml, "menutype");
            if (menutype == -1)
            {
                menutype = 1;
            }
        }
        return menutype;
    }

    private String loadString(String path)
    {
//        Class c = getClass();
//        if (path == null)
//        {       
//                return "";
//        }
//
//        InputStream is = null;
//        is = c.getResourceAsStream(path);
//        if (is==null)
//        {           
//            return "";
//        }
//
//        Reader reader;
//        try
//        {
//            reader = new InputStreamReader(is,"utf-8");
//logger.Logger.getInstance().log("utf-8");  +         
//        }
//        catch(Exception e)
//        {
//            reader =  new InputStreamReader(is);            
//        }
//
//        int ch;
//        StringBuffer result = new StringBuffer();
//
//        try
//        {
//            while ((ch = reader.read()) != -1)
//            {
//                result.append((char)ch);
//            }
//        }catch(Exception e){ add show log}
//show count of result
//        return result.toString();
        
        return InterfaceXML.xml;
    }

    private void loadXML()
    {
        xml = loadString(xmlPath);

        formsXML = XMLParser.getTags(xml, "form");
    }

    private DynamicForm generateForm(String name)
    {     
        for (int i = 0; i < formsXML.size(); ++i)
        {
            if (XMLParser.getParamString((String)formsXML.elementAt(i), "form_name").equals(name))
            {            
                return new DynamicForm((String)formsXML.elementAt(i));
            }
        }

        return null;
    }

    public DynamicForm getForm(String name)
    {
        if (formsList.containsKey(name))
        {          
            return (DynamicForm)formsList.get(name);
        }

        DynamicForm form = generateForm(name);
        if (form != null)
        {
            formsList.put(name, form);
        }
        
        return form;
    }
}
